package org.Shiikong_Memory.SakuraBlockFrame.BasisTool;

import org.bukkit.configuration.file.FileConfiguration;

import java.util.Objects;

public class LanguageModules {
    private ConfigTool configTool;
    private String currentLanguage;

    private LanguageModules() {
    }

    public static LanguageModules createLanguageModules(ConfigTool configTool) {
        LanguageModules languageModules = new LanguageModules();
        languageModules.configTool = configTool;
        configTool.createDefaultConfig("Lang", "lang-cn.yml", "lang-cn.yml");
        languageModules.setLanguage("cn");
        return languageModules;
    }

    /**
     * 设置需要获取的语言
     * @param language 语言标识，例如 "cn" 代表中文
     */
    public void setLanguage(String language) {
        this.currentLanguage = language;
    }

    /**
     * 获取指定的语句
     * @param key 语句的键
     * @return 指定语言下对应键的语句，如果未找到则返回空字符串
     */
    public String getMessage(String key) {
        if (configTool == null || currentLanguage == null) {
            return "";
        }
        String configFileName = "lang-" + currentLanguage;
        FileConfiguration config = configTool.getConfig("Lang", configFileName);
        return Objects.requireNonNull(config.getString(configFileName, key), "");
    }
    /**
     * 获取指定的语句
     * @param key 语句的键
     * @param defaultString 为空时返回默认语句
     * @return 指定语言下对应键的语句，如果未找到则返回空字符串
     */
    public String getMessage(String key, String defaultString) {
        if (configTool == null || currentLanguage == null) {
            return "";
        }
        String configFileName = "lang-" + currentLanguage;
        FileConfiguration config = configTool.getConfig("Lang", configFileName);
        return Objects.requireNonNull(config.getString(configFileName, key), defaultString);
    }
}
